package com.ws.controller;

import com.ws.bean.Depart;
import com.ws.service.DepartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class DepartController {

    @Autowired
    private DepartService departService;

    @RequestMapping("/findAllDepart")
    public String findAllDepart(String dstatus, Model model) {

        //调用service
        List<Depart> departs = departService.findByStatus(dstatus);
        //共享数据
        System.out.println(departs);
        model.addAttribute("departs", departs);
        //响应
        return "depart.jsp";
    }

    @RequestMapping("/addDepart")
    public String addDepart(String dname, String duty) {
        //调用service
        departService.addDepart(dname, duty);
        //响应
        return "findAllDepart?dstatus=0";
    }

    @RequestMapping("/findByDid")
    public String findByDid(String did, Model model) {
        Depart depart = departService.findByDid(did);
        model.addAttribute("depart", depart);
        System.out.println("****************************");
        System.out.println(depart);
        return "editDepart.jsp";
    }

    @RequestMapping("/updateDepart")
    public String updateDepart(Depart depart) {
        departService.updateDepart(depart);
        return "findAllDepart?dstatus=0";
    }

    @RequestMapping("/updateDstatus")
    public String updateDstatus(int did, int dstatus) {
        departService.updateDstatus(did, dstatus);
        return "findAllDepart?dstatus="+(dstatus==1?0:1);
    }

}
